package com.medicine.hbp.fragment;

import java.util.ArrayList;

import org.kymjs.kjframe.http.HttpParams;

import com.medicine.hbp.adapter.ClassificationAdapter;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.adapter.StellarMapAdapter;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.laberInfoListVo;
import com.medicine.hbp.ui.SearchActivity;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.shitou.googleplay.lib.randomlayout.StellarMap;
import com.medicine.hbp.R;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 搜索
 *
 * @author Administrator
 */
public class SearchFragment extends Fragment{
	private FrameLayout fl_txtfr;
	private StellarMap map;
	private View View;
	private EditText edit_search;
	private ImageView btn_search;
	private ArrayList<String> list = new ArrayList<String>();
	Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			edit_search.setText(msg.obj.toString());
			httpSearchLaber(msg.obj.toString());
		}
	};
	@Override
	public android.view.View onCreateView(LayoutInflater inflater,
										  ViewGroup container, Bundle savedInstanceState) {
		View = inflater.inflate(R.layout.fragment_search, container, false);
		fl_txtfr = (FrameLayout) View.findViewById(R.id.fl_txtfr);
		edit_search= (EditText) View.findViewById(R.id.edit_search);
		btn_search= (ImageView) View.findViewById(R.id.btn_search);

		initDate();
		httpSearchLaberOrder();
		return View;
	}

	private void initDate() {
		btn_search.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				String search=edit_search.getText().toString();
				if(TextUtils.isEmpty(search)){
					ToastUtil.showShort(getActivity(), "请输入您要搜索的内容");
					return;
				}
				httpSearchLaber(search);
			}
		});

	}

	/**
	 * 搜索
	 */
	private void httpSearchLaber(String context) {
		HttpParams params = new HttpParams();
		params.put("keyword", context);//用户输入需要搜索的文字
		AppHttps.HttpPost(getActivity(),HttpInterface.SEARCH_LABER, params,
				new AppHttpCallBack<artGroupListVo>(artGroupListVo.class,
						getActivity()) {
					@Override
					public void onSuccess(artGroupListVo result) {
						Intent intent=new Intent(getActivity(),SearchActivity.class);
						intent.putExtra("result", result);
						startActivity(intent);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}
	/**
	 * 搜索用标签获取
	 */
	private void httpSearchLaberOrder() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(getActivity(),HttpInterface.SEARCH_LABER_ORDER, params,
				new AppHttpCallBack<laberInfoListVo>(laberInfoListVo.class,
						getActivity()) {
					@Override
					public void onSuccess(laberInfoListVo result) {
						try {
							for (int i = 0; i < 1; i++) {
								for (int j = 0; j < result.laberInfo.size(); j++) {
									list.add(result.laberInfo.get(j).laberName);
								}
							}
							map = new StellarMap(getActivity());
							int padding = 15;
							map.setInnerPadding(padding, padding, padding, padding);
							map.setAdapter(new StellarMapAdapter(getActivity(), list,handler));
							// 设置默认显示第几组的数据
							map.setGroup(0, true);// 这里默认显示第0组
							// 设置x和y方向上的显示的密度
							map.setRegularity(2, 2);// 如果值设置的过大，有可能造成子View摆放比较稀疏
							fl_txtfr.addView(map);
						} catch (Exception e) {
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
				});

	}

}
